<?php

namespace App\Http\Controllers\Network;

use App\Http\Controllers\Wechat\BaseController;
use Illuminate\Http\Request;
use NetworkService;
use Validator;

class RegisterController extends BaseController
{

    public function index()
    {
        $selects = ['selects' => ['所属年级', '所属单位', '所在楼宇', '所在楼层', '所在房号']];

        $name = ['name' => [1, 3, 2, 4, 5]];

        try{
            $option = NetworkService::getCrawlInfo();
        }catch(\Exception $e){
            return view('errors.back');
        }

        return view('app.network.index', array_merge($option, $selects, $name));
    }

    public function submit(Request $request)
    {
        $messages = [
            'mainForm:fullName.required' => '姓名必须填写',
            'mainForm:certification.required' => '身份证号必须填写',
            'mainForm:userName.required' => '学号必须填写',
            'mainForm:userPasswd.required' => '账号密码必须填写',
        ];

        Validator::make($request->all(), [
            'mainForm:fullName' => 'required',
            'mainForm:certification' => 'required',
            'mainForm:userName' => 'required',
            'mainForm:userPasswd' => 'required',
        ], $messages);

        if(NetworkService::submit($request) === true){
            return response()->json(['msg' => 'ok', 'target' => config('wechat.url.network_tutorial')]);
        }

        return response()->json(['msg' => '该账号已被预注册']);
    }

}
